package com.adingxiong.cft.config.component;

import com.adingxiong.cft.common.entity.MinioItem;
import com.adingxiong.cft.config.ApplicationProperties;
import com.adingxiong.cft.config.MinioProperties;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName MinioTemplate
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/1/21 10:27
 **/
@Component
@RequiredArgsConstructor
public class MinioTemplate implements InitializingBean {

    @Autowired
    private  MinioProperties properties;

    @Autowired
    private ApplicationProperties applicationProperties;

    private static MinioClient client;

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            applicationProperties.getExpiration();
            client = new MinioClient(properties.getEndpoint(), properties.getAccessKey(), properties.getSecretKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 检查文件存储通道是否存在
     *
     * @param bucketName
     * @return
     */
    @SneakyThrows
    public boolean exsitBucket(String bucketName) {
        return client.bucketExists(bucketName);
    }

    /**
     * 创建存储通道
     *
     * @param bucketName
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
        if (!exsitBucket(bucketName)) {
            client.makeBucket(bucketName);
        }
    }

    /**
     * 获取所有的存储通道
     *
     * @return
     */
    @SneakyThrows
    public List<Bucket> getAll() {
        return client.listBuckets();
    }

    /**
     * 根据通道名获取通道信息
     *
     * @param bucketName
     * @return
     */
    public Optional<Bucket> getBucket(String bucketName) {
        List<Bucket> all = getAll();
        return all.stream().filter(v -> v.name().equals(bucketName)).findFirst();
    }

    @SneakyThrows
    public void removeBucket(String bucketName) {
        client.removeBucket(bucketName);
    }

    /**
     * 根据后缀和通道名获取资源
     *
     * @param bucketName
     * @param suffix
     * @param recurisive
     * @return
     */
    @SneakyThrows
    public List<MinioItem> getAllBuySuffix(String bucketName, String suffix, boolean recurisive) {
        List<MinioItem> list = new ArrayList<>();
        Iterable<Result<Item>> results = client.listObjects(bucketName, suffix, recurisive);
        for (Result<Item> res : results) {
            list.add(new MinioItem(res.get()));
        }
        return list;
    }


    /**
     * 获取文件外链
     *
     * @param buketName
     * @param objectName
     * @param expires
     * @return
     */
    @SneakyThrows
    public String getNetUrl(String buketName, String objectName, Integer expires) {
        return client.presignedGetObject(buketName, objectName, expires);
    }
    @SneakyThrows
    public String getNetUrl(String bucketName, String ObjectName) {
        return client.presignedGetObject(bucketName, ObjectName);
    }

    /**
     * 获取文件流
     * @param bucketName
     * @param objectName
     * @return
     */
    @SneakyThrows
    public InputStream getFile(String bucketName,String objectName) {
        return client.getObject(bucketName, objectName);
    }

    /**
     * 文件上传
     * @param buketName
     * @param objectName
     * @param stream  返回的是下载路径,无法预览
     */
    @SneakyThrows
    public void putObject(String buketName,String objectName,InputStream stream){
        client.putObject(buketName,objectName,stream,stream.available(),"application/octet-stream");
    }

    @SneakyThrows
    public void putObject(String bucetName,String objectName,InputStream stream,long size,String context) {
        client.putObject(bucetName, objectName, stream, size, context);
    }

    /**
     * 返回预览的路径
     * @param bucketName
     * @param s
     * @param inputStream
     * @param contentType
     */
    @SneakyThrows
    public void putObject(String bucketName, String s, InputStream inputStream, String contentType) {
        client.putObject(bucketName, s, inputStream, contentType);
    }

    /**
     * 获取文件信息
     * @param buectName
     * @param objectName
     * @return
     */
    @SneakyThrows
    public ObjectStat getObejct(String buectName,String objectName){
        return client.statObject(buectName, objectName);
    }

    @SneakyThrows
    public void delObject(String bucketName,String objectName) {
        client.removeObject(bucketName,objectName);
    }



}
